package com.yowasa.xiuxian.engine.travel.adventure;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.yowasa.xiuxian.engine.CommonEngine;
import com.yowasa.xiuxian.engine.item.ItemInterface;
import com.yowasa.xiuxian.engine.travel.AdventureInterface;
import com.yowasa.xiuxian.entity.bo.user.UserBO;
import com.yowasa.xiuxian.entity.enums.MapEnum;
import com.yowasa.xiuxian.entity.po.XiuxianTravelShop;
import com.yowasa.xiuxian.mapper.XiuxianTravelShopMapper;
import com.yowasa.xiuxian.service.helper.ItemHelper;
import com.yowasa.xiuxian.util.RandomUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 木枯藤获取
 */
@Component
public class YouShang implements AdventureInterface {
    @Autowired
    private XiuxianTravelShopMapper xiuxianTravelShopMapper;


    @Override
    public Set<MapEnum> limitMaps() {
        return Sets.newHashSet(MapEnum.DI_HUA_ZHOU,MapEnum.WANG_SHU_KE_ZHAN,MapEnum.GUI_LI_YUAN,MapEnum.LI_YUE_GANG,MapEnum.DUN_YU_LING,MapEnum.QING_CE_ZHUANG,MapEnum.LU_HUA_CHI);
    }

    public boolean limitCheck(UserBO user) {
        Integer local = user.getLocal();
        List<XiuxianTravelShop> xiuxianTravelShops = xiuxianTravelShopMapper.selectList(new LambdaQueryWrapper<XiuxianTravelShop>()
                .gt(XiuxianTravelShop::getEndTime, new Date())
                .eq(XiuxianTravelShop::getMapId,local));
        if (xiuxianTravelShops.size() != 0){
            return false;
        }
        return true;
    }

    @Override
    public String exec(UserBO user) {
        Integer local = user.getLocal();
        List<XiuxianTravelShop> xiuxianTravelShops = xiuxianTravelShopMapper.selectList(new LambdaQueryWrapper<XiuxianTravelShop>()
                .gt(XiuxianTravelShop::getEndTime, new Date())
                .eq(XiuxianTravelShop::getMapId,local));
        if (xiuxianTravelShops.size() != 0){
            return "你遇见了从枫丹来的游商，他本想向你兜售他的商品，可是这里已经有别的游商了，将他赶跑了";
        }else {
            //0 - 2
            int i = new Random().nextInt(3);
            String choice ;
            if(i == 0){
                choice = RandomUtil.choice(Lists.newArrayList("北陆武器原胚", "北陆法器原胚", "生之花", "时之沙", "理之冠", "空之杯", "死之羽"));
            }else if(i == 1){
                choice = RandomUtil.choice(Lists.newArrayList("天空之刃图纸", "四风原典图纸", "乐团的晨光图纸", "终幕的时计图纸", "指挥的礼帽图纸", "吟游者之壶图纸", "琴师的箭羽图纸"));
            }else {
                choice = RandomUtil.choice(Lists.newArrayList("长生丹", "淬骨丹", "启智丹", "感气丹", "紫金丹","洗髓丹","神形再造丹","脱胎换骨丹"));
            }
            ItemInterface itemInterface = CommonEngine.ITEM_MAP.get(choice);
            XiuxianTravelShop xiuxianTravelShop = new XiuxianTravelShop();

            Date creatTime = new Date();
            xiuxianTravelShop.setStartTime(creatTime);
            //设置生效时间为一小时后
            Calendar cal = Calendar.getInstance();
            cal.setTime(creatTime);
            cal.add(Calendar.HOUR, 1);// 24小时制
            Date endTime = cal.getTime();
            xiuxianTravelShop.setEndTime(endTime);
            xiuxianTravelShop.setItemName(itemInterface.getName());
            xiuxianTravelShop.setMapId(local.longValue());
            xiuxianTravelShop.setItemNum(1);
            xiuxianTravelShop.setItemType(itemInterface.getType().getId());
            xiuxianTravelShop.setItemPrice((int) (itemInterface.getPrice() * 0.8));
            xiuxianTravelShopMapper.insert(xiuxianTravelShop);

            Set<String> strings = CommonEngine.ITEM_MAP.keySet();
            List<String> collect = strings.stream().collect(Collectors.toList());
            String s = randomGet(collect);
            ItemInterface anInterface = CommonEngine.ITEM_MAP.get(s);
            XiuxianTravelShop shop = new XiuxianTravelShop();
            Date newtime = new Date();
            shop.setStartTime(newtime);
            shop.setEndTime(endTime);
            shop.setMapId(local.longValue());
            shop.setItemNum(1);
            shop.setItemPrice(anInterface.getPrice());
            shop.setItemName(anInterface.getName());
            shop.setItemType(anInterface.getType().getId());
            xiuxianTravelShopMapper.insert(shop);
            return "你遇见了从枫丹来的游商，他可带来了不少宝贝，可使用指令 /游商 查看详情\n注：游商必定出售灵药，高级装备图纸，原胚中的一种\n灵药效果强大，有逆天改命之能，故每种灵药有服用次数上限";
        }

    }

    private String randomGet(List<String> collect) {

        String s = cn.hutool.core.util.RandomUtil.randomEle(collect);
        ArrayList<String> list = Lists.newArrayList("千岩同心", "千风之诗", "狼的末路","护摩之杖","雾切之回光", "神乐之真意","飞雷之弦振", "薙草之稻光");
        if(list.contains(s)){
            s = randomGet(collect);
        }
        return s;
    }


}
